
from singleton import singleton

@singleton
class EventDispatcher(object):

    def __init__(self):
        self.listeners = {}

    def addListener(self, eventType, listener):
        if eventType in self.listeners:
            self.listeners[eventType].append(listener)
        else:
            self.listeners[eventType] = [listener]

    def removeListener(self, eventType, listener):
        if eventType in self.listeners:
            self.listeners[eventType].remove(listener)

    def dispatchEvent(self, eventType, *args, **kwargs):
        if eventType in self.listeners:
            for listener in self.listeners[eventType]:
                listener(*args, **kwargs)

Event_Dispatcher = EventDispatcher()